package org.mspring.mlog.web.module.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.security.User;
import org.mspring.mlog.service.MessageService;
import org.mspring.mlog.web.security.SecurityUtils;
import org.mspring.platform.web.ResponseEntity;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 站内消息
 * 
 * @author Gao Youbo
 * @since 2013年7月22日
 */
@Widget
@RequestMapping("/message")
public class Web_MessageWidget extends Web_AbstractWidget {
    @Autowired
    private MessageService messageService;

    /**
     * 统计当前登录用户有多少条未读消息
     * 
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/count")
    @ResponseBody
    public ResponseEntity count(HttpServletRequest request, HttpServletResponse response, Model model) {
        ResponseEntity rsp = new ResponseEntity();
        User user = SecurityUtils.getCurrentUser(request);
        if (user == null) {
            rsp.setSuccess(false);
            rsp.setMessage("用户未登录");
            return rsp;
        }
        Integer count = messageService.getUnReadMessageCount(user.getId());
        if (count == null) {
            count = 0;
        }
        rsp.setSuccess(true);
        rsp.put("count", count);
        return rsp;
    }
}
